package com.tencet.dao.impl;

import com.tencet.dao.TransferDAO;
import com.tencet.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: @date: 2022/7/21 16:00
 * @description:
 */
public class TransferDAOImpl implements TransferDAO {

    QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());

    @Override
    public List getCenterByName(String p_name) {
        String sql = "select card_num from bank_account1 where account_id2=?";

        try {

            List account_id1s = (List) qr.query( sql, new ScalarHandler(), p_name);
            return account_id1s;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean changeBalance(int money, String reduce_num, String add_num) {
        AccountOneDAOImpl accountOneDAO = new AccountOneDAOImpl();

        try {
            Integer balance = accountOneDAO.getBalanceByIdOne(reduce_num);
            balance = balance - money;
            String sql = "update bank_account1 set balance = ? where account_id1=?";
            qr.update( sql, balance, reduce_num);

            balance = accountOneDAO.getBalanceByIdOne(add_num);
            balance = balance + money;
            qr.update(sql, balance, add_num);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }

    @Override
    public String getNameByPhone(String phone) {
        String sql = "select account_id2 from bank_account2 where phone_num=?";

        try {

            List account_id2 = (List) qr.query(sql, new ScalarHandler(), phone);
            sql = "select card_num from bank_account1 where account_id2=?";

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean getCardNum(String reduce_num) {
        String sql = "select * from bank_account1 where card_num=?";

        boolean b = false;
        try {

            Object o = qr.query(sql, new ScalarHandler(), reduce_num);
            if (o != null) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}